﻿using System;
using HslCommunication.Core;

namespace HslCommunication.Profinet.Delta
{
	/// <summary>
	/// 台达PLC的相关的帮助类，公共的地址解析的方法。<br />
	/// Delta PLC related help classes, public address resolution methods.
	/// </summary>
	// Token: 0x020000AE RID: 174
	public class DeltaHelper
	{
		/// <summary>
		/// 根据台达PLC的地址，解析出转换后的modbus协议信息，适用DVP系列，当前的地址仍然支持站号指定，例如s=2;D100<br />
		/// According to the address of Delta PLC, the converted modbus protocol information is parsed out, applicable to DVP series, 
		/// the current address still supports station number designation, such as s=2;D100
		/// </summary>
		/// <param name="address">台达plc的地址信息</param>
		/// <param name="modbusCode">原始的对应的modbus信息</param>
		/// <returns>还原后的modbus地址</returns>
		// Token: 0x06000CBB RID: 3259 RVA: 0x0004BC38 File Offset: 0x00049E38
		public static OperateResult<string> PraseDeltaDvpAddress(string address, byte modbusCode)
		{
			OperateResult<string> result;
			try
			{
				string str = string.Empty;
				OperateResult<int> operateResult = HslHelper.ExtractParameter(ref address, "s");
				bool isSuccess = operateResult.IsSuccess;
				if (isSuccess)
				{
					str = string.Format("s={0};", operateResult.Content);
				}
				bool flag = modbusCode == 1 || modbusCode == 15 || modbusCode == 5;
				if (flag)
				{
					bool flag2 = address.StartsWith("S") || address.StartsWith("s");
					if (flag2)
					{
						return OperateResult.CreateSuccessResult<string>(str + Convert.ToInt32(address.Substring(1)).ToString());
					}
					bool flag3 = address.StartsWith("X") || address.StartsWith("x");
					if (flag3)
					{
						return OperateResult.CreateSuccessResult<string>(str + "x=2;" + (Convert.ToInt32(address.Substring(1), 8) + 1024).ToString());
					}
					bool flag4 = address.StartsWith("Y") || address.StartsWith("y");
					if (flag4)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1), 8) + 1280).ToString());
					}
					bool flag5 = address.StartsWith("T") || address.StartsWith("t");
					if (flag5)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 1536).ToString());
					}
					bool flag6 = address.StartsWith("C") || address.StartsWith("c");
					if (flag6)
					{
						return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 3584).ToString());
					}
					bool flag7 = address.StartsWith("M") || address.StartsWith("m");
					if (flag7)
					{
						int num = Convert.ToInt32(address.Substring(1));
						bool flag8 = num >= 1536;
						if (flag8)
						{
							return OperateResult.CreateSuccessResult<string>(str + (num - 1536 + 45056).ToString());
						}
						return OperateResult.CreateSuccessResult<string>(str + (num + 2048).ToString());
					}
				}
				else
				{
					bool flag9 = address.StartsWith("D") || address.StartsWith("d");
					if (flag9)
					{
						int num2 = Convert.ToInt32(address.Substring(1));
						bool flag10 = num2 >= 4096;
						if (flag10)
						{
							return OperateResult.CreateSuccessResult<string>(str + (num2 - 4096 + 36864).ToString());
						}
						return OperateResult.CreateSuccessResult<string>(str + (num2 + 4096).ToString());
					}
					else
					{
						bool flag11 = address.StartsWith("C") || address.StartsWith("c");
						if (flag11)
						{
							int num3 = Convert.ToInt32(address.Substring(1));
							bool flag12 = num3 >= 200;
							if (flag12)
							{
								return OperateResult.CreateSuccessResult<string>(str + (num3 - 200 + 3784).ToString());
							}
							return OperateResult.CreateSuccessResult<string>(str + (num3 + 3584).ToString());
						}
						else
						{
							bool flag13 = address.StartsWith("T") || address.StartsWith("t");
							if (flag13)
							{
								return OperateResult.CreateSuccessResult<string>(str + (Convert.ToInt32(address.Substring(1)) + 1536).ToString());
							}
						}
					}
				}
				result = new OperateResult<string>(StringResources.Language.NotSupportedDataType);
			}
			catch (Exception ex)
			{
				result = new OperateResult<string>(ex.Message);
			}
			return result;
		}
	}
}
